#include "wenjianshuju.h"
#include "ui_wenjianshuju.h"
#include<qdebug.h>
#include<QSqlRecord>

wenjianshuju::wenjianshuju(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::wenjianshuju)
{
    ui->setupUi(this);
    this->setWindowTitle("多任务列表");

}

wenjianshuju::~wenjianshuju()
{
    delete ui;
}

void wenjianshuju::shanchubiao()
{
    qDebug()<<"void wenjianshuju::shanchubiao()";

    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
       mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku_filename.db");

    if(mysql.open()==true)
    {
        QSqlQuery query(mysql);
        query.exec("DROP TABLE filename");

        //关闭
        mysql.close();
    }
    else
    {
        qDebug()<<"打开数据库失败";
    }

}

void wenjianshuju::updata()
{
    qDebug()<<"void wenjianshuju::updata()";
    QString eng[5];
    QString chi[5];
    QString tit[5];
    QString yinping[5];

    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
       mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku_filename.db");
    if(mysql.open()==true)
    {
        for(int i=0;i<5;i++)
        {
            QSqlQuery query(mysql);
            query.exec(QString("select * from filename where id = %1").arg(i));
            if(query.first()==true)
            {
                eng[i]=query.value("eng").toString();qDebug()<<"eng"<<eng[i];
                chi[i]=query.value("chi").toString();
                tit[i]=query.value("title").toString();
                yinping[i]=query.value("yinping").toString();
            }
        }

        model.setTable("filename");
        ui->tableView->setModel(&model);
        model.select();
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

        mysql.close();
    }


    ui->lineEdit_1->setPlainText(eng[0]);
    ui->lineEdit_2->setPlainText(chi[0]);
    ui->lineEdit_3->setPlainText(tit[0]);
    ui->yiping_0->setPlainText(yinping[0]);

    ui->lineEdit_4->setPlainText(eng[1]);
    ui->lineEdit_5->setPlainText(chi[1]);
    ui->lineEdit_6->setPlainText(tit[1]);
    ui->yiping_1->setPlainText(yinping[1]);

    ui->lineEdit_7->setPlainText(eng[2]);
    ui->lineEdit_8->setPlainText(chi[2]);
    ui->lineEdit_9->setPlainText(tit[2]);
    ui->yiping_2->setPlainText(yinping[2]);

    ui->lineEdit_10->setPlainText(eng[3]);
    ui->lineEdit_11->setPlainText(chi[3]);
    ui->lineEdit_12->setPlainText(tit[3]);
    ui->yiping_3->setPlainText(yinping[3]);

    ui->lineEdit_13->setPlainText(eng[4]);
    ui->lineEdit_14->setPlainText(chi[4]);
    ui->lineEdit_15->setPlainText(tit[4]);
    ui->yiping_4->setPlainText(yinping[4]);

}

void wenjianshuju::on_pushButton_clicked()
{

    QString eng[5];
    QString chi[5];
    QString tit[5];
    QString yinping[5];

    if(ui->lineEdit_1->toPlainText().left(8)=="file:///")
    {
         eng[0]=ui->lineEdit_1->toPlainText().right(ui->lineEdit_1->toPlainText().length()-8);
    }
    else {
        eng[0]=ui->lineEdit_1->toPlainText();
    }

    if(ui->lineEdit_2->toPlainText().left(8)=="file:///")
    {
        chi[0]=ui->lineEdit_2->toPlainText().right(ui->lineEdit_2->toPlainText().length()-8);
    }
    else
    {
        chi[0]=ui->lineEdit_2->toPlainText();
    }

    tit[0]=ui->lineEdit_3->toPlainText();

    if(ui->yiping_0->toPlainText().left(8)=="file:///")
    {
        yinping[0]=ui->yiping_0->toPlainText().right(ui->yiping_0->toPlainText().length()-8);
    }
    else
    {
        yinping[0]=ui->yiping_0->toPlainText();
    }
    //

    if(ui->lineEdit_4->toPlainText().left(8)=="file:///")
    {
        eng[1]=ui->lineEdit_4->toPlainText().right(ui->lineEdit_4->toPlainText().length()-8);
    }
    else
    {
        eng[1]=ui->lineEdit_4->toPlainText();
    }

    if(ui->lineEdit_5->toPlainText().left(8)=="file:///")
    {
        chi[1]=ui->lineEdit_5->toPlainText().right(ui->lineEdit_5->toPlainText().length()-8);
    }
    else
    {
        chi[1]=ui->lineEdit_5->toPlainText();
    }
    tit[1]=ui->lineEdit_6->toPlainText();

    if(ui->yiping_1->toPlainText().left(8)=="file:///")
    {
        yinping[1]=ui->yiping_1->toPlainText().right(ui->yiping_1->toPlainText().length()-8);
    }
    else
    {
        yinping[1]=ui->yiping_1->toPlainText();
    }
    //

        if(ui->lineEdit_7->toPlainText().left(8)=="file:///")
        {
            eng[2]=ui->lineEdit_7->toPlainText().right(ui->lineEdit_7->toPlainText().length()-8);
        }
        else
        {
            eng[2]=ui->lineEdit_7->toPlainText();
        }

    if(ui->lineEdit_8->toPlainText().left(8)=="file:///")
    {
        chi[2]=ui->lineEdit_8->toPlainText().right(ui->lineEdit_8->toPlainText().length()-8);
    }
    else
    {
        chi[2]=ui->lineEdit_8->toPlainText();
    }

    tit[2]=ui->lineEdit_9->toPlainText();

    if(ui->yiping_2->toPlainText().left(8)=="file:///")
    {
        yinping[2]=ui->yiping_2->toPlainText().right(ui->yiping_2->toPlainText().length()-8);
    }
    else
    {
        yinping[2]=ui->yiping_2->toPlainText();
    }
    //

    if(ui->lineEdit_10->toPlainText().left(8)=="file:///")
    {
        eng[3]=ui->lineEdit_10->toPlainText().right(ui->lineEdit_10->toPlainText().length()-8);
    }
    else
    {
        eng[3]=ui->lineEdit_10->toPlainText();
    }

    if(ui->lineEdit_11->toPlainText().left(8)=="file:///")
    {
        chi[3]=ui->lineEdit_11->toPlainText().right(ui->lineEdit_11->toPlainText().length()-8);
    }
    else
    {
        chi[3]=ui->lineEdit_11->toPlainText();
    }

    tit[3]=ui->lineEdit_12->toPlainText();

    if(ui->yiping_3->toPlainText().left(8)=="file:///")
    {
        yinping[3]=ui->yiping_3->toPlainText().right(ui->yiping_3->toPlainText().length()-8);
    }
    else
    {
        yinping[3]=ui->yiping_3->toPlainText();
    }
    //

    if(ui->lineEdit_13->toPlainText().left(8)=="file:///")
    {
        eng[4]=ui->lineEdit_13->toPlainText().right(ui->lineEdit_13->toPlainText().length()-8);
    }
    else
    {
        eng[4]=ui->lineEdit_13->toPlainText();
    }

    if(ui->lineEdit_14->toPlainText().left(8)=="file:///")
    {
        chi[4]=ui->lineEdit_14->toPlainText().right(ui->lineEdit_14->toPlainText().length()-8);
    }
    else
    {
        chi[4]=ui->lineEdit_14->toPlainText();
    }

    tit[4]=ui->lineEdit_15->toPlainText();

    if(ui->yiping_4->toPlainText().left(8)=="file:///")
    {
        yinping[4]=ui->yiping_4->toPlainText().right(ui->yiping_4->toPlainText().length()-8);
    }
    else
    {
        yinping[4]=ui->yiping_4->toPlainText();
    }

    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
       mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku_filename.db");

    this->shanchubiao();

    //解决单引号
    for(int i= 0;i<5;i++)
    {
        eng[i].replace("'","''");
        chi[i].replace("'","''");
        tit[i].replace("'","''");
        yinping[i].replace("'","''");
    }

    if( mysql.open()==true)
    {
        QSqlQuery query(mysql);
        query.exec("create table filename(id int, eng varchar(500),chi varchar(500),title varchar(200) , yinping varchar(500))");
        for(int i=0;i<5;i++)
        {

            query.exec(QString("insert into filename(id,eng,chi,title,yinping) values(%1,'%2','%3','%4','%5')").arg(i).arg(eng[i]).arg(chi[i]).arg(tit[i]).arg(yinping[i]));
            qDebug()<<QString("insert into filename(id,eng,chi,title,yinping) values(%1,'%2','%3','%4','%5')").arg(i).arg(eng[i]).arg(chi[i]).arg(tit[i]).arg(yinping[i]);
        }

        model.setTable("filename");
        ui->tableView->setModel(&model);
        model.select();
        ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

        mysql.close();

    }
    else {
        QMessageBox::warning(this,"错误",mysql.lastError().text());
        return;
    }


    return;
}

void wenjianshuju::on_pushButton_start_clicked()
{
    this->hide();
    emit signal_sddover();

    return;
}

void wenjianshuju::on_pushButton_genxin_clicked()
{
    this->updata();
}

void wenjianshuju::on_pushButton_deletedata_clicked()
{
    this->shanchubiao();
    this->updata();
}
